/*
 * @Author: zxc
 * @Date: 2020-11-18 09:04:03
 * @LastEditTime: 2020-11-18 10:01:01
 * @LastEditors: zxc
 * @Description:中介者模式
 */
// class Player {
//   constructor(name) {
//     this.name = name
//     this.enemy = null
//   }
//   win() {
//     console.log(this.name + "  win")
//   }
//   lose() {
//     console.log(this.name + "  lose")
//   }
//   die() {
//     this.lose()
//     this.enemy.win()
//   }
// }

// let player1 = new Player("狗子")
// let player2 = new Player("二狗")

// player1.enemy = player2
// player2.enemy = player1

// player1.die()

class Player {
  constructor(name, teamColor) {
    this.name = name
    this.teamColor = teamColor
    this.state = "alive"
  }
  win() {
    console.log(this.name + "  win")
  }
  lose() {
    console.log(this.name + "  lose")
  }
  die() {
    this.state = "dead"
    playerDirector.reciveMessage("playerDead", this)
  }
  remove() {
    playerDirector.reciveMessage("removePlayer", this)
  }
  changeTeam(color) {
    playerDirector.reciveMessage("changeTeam", this, color)
  }
}

let playerFactory = function (name, teamColor) {
  let newPlayer = new Player(name, teamColor)
  playerDirector.reciveMessage("addPlayer", newPlayer)
  return newPlayer
}

let playerDirector = (function () {
  let players = {},
    operations = {}
  operations.addPlayer = function (player) {
    let teamColor = player.teamColor
    players[teamColor] = players[teamColor] || []
    players[teamColor].push(player)
  }
  operations.removePlayer = function (player) {
    let teamColor = player.teamColor
    let teamPlayers = players[teamColor] || []
    players[teamColor] = teamPlayers.filter(x => x !== player)
    // for (var i = teamPlayers.length - 1; i >= 0; i--) {
    //   if(teamPlayers[i] === player){
    //     teamPlayers.splice
    //   }
    // }
  }
  operations.changeTeam = function (player, newTeamColor) {
    operations.removePlayer(player)
    player.teamColor = newTeamColor
    operations.addPlayer(player)
  }

  operations.playerDead = function (player) {
    let teamColor = player.teamColor,
      teamPlayers = players[teamColor],
      all_dead = true
    all_dead = teamPlayers.every(x => x.state === "dead")

    if (all_dead) {
      teamPlayers.forEach(x => x.lose())
      for (let color in players) {
        if (color !== teamColor) {
          let otherTeamPlayers = players[color]
          otherTeamPlayers.forEach(x => x.win())
        }
      }
    }
  }

  let reciveMessage = function (...args) {
    let message = args.shift()
    operations[message].apply(this, args)
  }
  return {
    reciveMessage
  }
})()

let player1 = playerFactory("1狗", "red"),
  player2 = playerFactory("2狗", "red"),
  player3 = playerFactory("3狗", "red"),
  player4 = playerFactory("4狗", "red")

let player5 = playerFactory("5蛋", "blue"),
  player6 = playerFactory("6蛋", "blue"),
  player7 = playerFactory("7蛋", "blue"),
  player8 = playerFactory("8蛋", "blue")

player1.changeTeam("blue")
player2.remove()
player3.die()
player4.die()
